package com.gensdai.leliang.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gensdai.leliang.R;
import com.gensdai.leliang.base.BaseActivityNoAbs;
import com.gensdai.leliang.dialog.Upload;
import com.gensdai.leliang.json_base.json_base;
import com.gensdai.leliang.remoteInterface.ApiService;
import com.trello.rxlifecycle2.android.ActivityEvent;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Administrator on 2017/9/8.
 */

public class Set_yunyu extends BaseActivityNoAbs implements View.OnClickListener {
    @BindView(R.id.ui_title)
    TextView uiTitle;
    @BindView(R.id.back)
    LinearLayout back;
    @BindView(R.id.ybb)
    TextView ybb;
    @BindView(R.id.hyz)
    TextView hyz;
    @BindView(R.id.byz)
    TextView byz;
    @BindView(R.id.one)
    RelativeLayout one;
    @BindView(R.id.xgz)
    TextView xgz;
    @BindView(R.id.xwz)
    TextView xwz;
    @BindView(R.id.two)
    RelativeLayout two;

    private Upload up;
    SharedPreferences share;
    SharedPreferences.Editor ed;

    private final int JIECHU = 3;// 设置状态
    private Handler handler = new Handler() {
        @Override
        public void dispatchMessage(Message msg) {
            switch (msg.what) {
                case JIECHU:
                    up.dismiss();
                    try {
                        if (new json_base().GOOD((String) msg.obj)) {
                            Set_yunyu.this.finish();
                        } else {

                        }
                        Toast.makeText(
                                Set_yunyu.this,
                                new JSONObject((String) msg.obj)
                                        .getString("message"),
                                Toast.LENGTH_SHORT).show();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.set_yunyu_state);
        ButterKnife.bind(this);
        init();
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    /**
     * 初始化
     */
    private void init() {

        uiTitle.setText("状态设置");
        share = getSharedPreferences("User", MODE_PRIVATE);
        ed = share.edit();
        up = new Upload(this);
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.danru);
        one.startAnimation(animation);

        back.setOnClickListener(this);
        ybb.setOnClickListener(this);
        hyz.setOnClickListener(this);
        byz.setOnClickListener(this);
        xwz.setOnClickListener(this);
        xgz.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back:
                this.finish();
                break;
            case R.id.ybb:
//                Animation animation = AnimationUtils.loadAnimation(this, R.anim.danchu);
//                one.startAnimation(animation);
                one.setVisibility(View.GONE);
                two.setVisibility(View.VISIBLE);
//                Animation animations = AnimationUtils.loadAnimation(this, R.anim.danchu);
//                one.startAnimation(animations);
                uiTitle.setText("宝宝性别");
                break;
            case R.id.hyz:
                Intent go_hyz = new Intent(this, Set_yunyu_xq.class);
                go_hyz.putExtra("title", "预产期设置");
                go_hyz.putExtra("state", "2");
                startActivity(go_hyz);
                this.overridePendingTransition(R.anim.danru,R.anim.danchu);
                this.finish();
                break;
            case R.id.byz:
                Map<String, String> loginParams = new HashMap<>();
                loginParams.put("state", "1");
                loginParams.put("userNo", share.getString("userno", ""));
                bangdingAPI(loginParams);
//                Intent go_byz = new Intent(this, Set_yunyu_xq.class);
//                go_byz.putExtra("title", "我的宝宝");
//                go_byz.putExtra("state", "3");
//                startActivity(go_byz);
//                this.finish();
                break;
            case R.id.xwz:
                Intent go_xwz = new Intent(this, Set_yunyu_xq.class);
                go_xwz.putExtra("title", "宝宝生日");
                go_xwz.putExtra("bb_sex", "1");
                go_xwz.putExtra("state", "1");
                startActivity(go_xwz);
                this.overridePendingTransition(R.anim.danru,R.anim.danchu);
                this.finish();
                break;
            case R.id.xgz:
                Intent go_xgz = new Intent(this, Set_yunyu_xq.class);
                go_xgz.putExtra("title", "宝宝生日");
                go_xgz.putExtra("bb_sex", "0");
                go_xgz.putExtra("state", "1");
                startActivity(go_xgz);
                //屏幕动画淡入淡出效果切换，调用anim文件夹中创建的enteralpha（进入动画）和exitalpha（淡出动画）两个动画（注意：两个xml文件命名不能有大写字母）
                //如果想定义其他动画效果，只需要改变enteralpha和exitalpha两个文件
                this.overridePendingTransition(R.anim.danru,R.anim.danchu);
                this.finish();
                break;
            default:
                break;
        }
    }

    /**
     * 设置绑定
     */
    private void bangdingAPI(Map<String, String> loginParams) {
        up.show();
        ApiService.getInstance(this).SET_PERSONAL_GESTATION(loginParams)
                //涉及到IO操作
                .subscribeOn(Schedulers.io())
                .compose(this.<String>bindUntilEvent(ActivityEvent.DESTROY)) //跟生命周期绑定一起
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(@NonNull Disposable disposable) throws Exception {
                        //开始监听前的处理，一般用于显示dialog什么的
////                        if (hud != null) {
////                            hud.show();
////                        }

                        Log.d("accept", "accept");
                    }
                })
                .subscribeOn(AndroidSchedulers.mainThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        //可以取消订阅
                        //还可以判断是否处于取消状态
                        //boolean b=d.isDisposed();

                        Log.d("onSubscribe", "onSubscribe");
                    }

                    @Override
                    public void onNext(@NonNull String s) {
                        //处理得到的数据
                        Log.e("quick_login", s);
                        Message msg = new Message();
                        msg.what = JIECHU;
                        msg.obj = s;
                        handler.sendMessage(msg);
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        //异常处理
                        up.dismiss();
                        Log.e("onError", e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        //监听完毕后的处理
                        Log.d("onComplete", "onComplete");
                    }
                });
    }


}
